package errorx

import (
	"fmt"
)

// BizError 业务错误
type BizError struct {
	code string
	err  string
	data interface{}
}

const (
	CODE_SUCCESS = "200"
	CODE_ERROR   = "500"
)

// 错误消息
func (e BizError) Error() string {
	return e.err
}

// Code  错误码
func (e BizError) Code() string {
	return e.code
}
func (e BizError) Data() interface{} {
	return e.data
}

func (e BizError) String() string {
	return fmt.Sprintf("errCode: %s, errMsg: %s", e.Code(), e.Error())
}

// NewBiz 创建业务逻辑错误结构体，默认为业务逻辑错误
func NewBiz(msg string, formats ...any) BizError {
	return BizError{code: CODE_ERROR, err: fmt.Sprintf(msg, formats...)}
}
func NewBizData(data interface{}, msg string, formats ...any) BizError {
	return BizError{code: CODE_ERROR, err: fmt.Sprintf(msg, formats...), data: data}
}

// NewBizCodeData 创建业务逻辑错误结构体，可设置指定错误code
func NewBizCodeData(code string, data interface{}, msg string, formats ...any) BizError {
	return BizError{code: code, err: fmt.Sprintf(msg, formats...), data: data}
}
